package com.zzyl.intercept;

import com.zzyl.constant.SecurityConstant;
import com.zzyl.constant.UserCacheConstant;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.UserThreadLocal;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.invoke.MethodHandle;

/**
 *  多租户放到SubjectContent上下文中
 */
@Component
public class UserTokenIntercept implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(UserTokenIntercept.class);
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;

    /*@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("222");
*//*        if (!(request instanceof HandlerMethod)) {
            return true;
        }*//*

        String userUUID = request.getHeader(SecurityConstant.USER_TOKEN);
        if (!StringUtils.hasText(userUUID)) {
            throw new Exception("用户登录状态异常");
        }

        String jwtTokenKey = UserCacheConstant.JWT_TOKEN + userUUID;
        String jwtToken = redisTemplate.opsForValue().get(jwtTokenKey);
        if (!StringUtils.hasText(jwtToken)) {
            throw new Exception("用户登录状态异常");
        }

        Claims claims = JwtUtil.parseJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(), jwtToken);
        String user = claims.get("currentUser").toString();
        UserThreadLocal.setSubject(user);
        log.info("222222");
        return true;
    }*/


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserThreadLocal.setSubject("{\"createBy\":1668522280851951617,\"createTime\":\"2023-06-19T23:11:17\",\"deptNo\":\"100001000000000\",\"email\":\"admin@qq.com\",\"id\":1671403256519078138,\"mobile\":\"15156409998\",\"nickName\":\"超级管理员\",\"password\":\"\",\"postNo\":\"100001001000000\",\"realName\":\"超级管理员\",\"remark\":\"\",\"resourceRequestPaths\":[\"PUT/alert-rule/**\",\"PATCH /role\",\"DELETE/nursingLevel/delete/**\",\"DELETE/iot/DeleteDevice\",\"DELETE/alert-rule/delete/**\",\"PUT/dept\",\"PATCH/resource\",\"POST/roomTypes\",\"DELETE/roomTypes/**\",\"POST /iot/UpdateDevice\",\"POST/alert-rule/create\",\"PUT/room/update\",\"PUT /elder/setNursing\",\"GET /room/get/**\",\"PUT/elder/setNursing\",\"POST/resource/tree\",\"DELETE /post/**\",\"DELETE/floor/delete/**\",\"POST/bed/create\",\"PUT/nursing/plan/**\",\"PUT/bill/cancel/**\",\"DELETE/role/**\",\"PUT/nursingTask/do\",\"PUT /alert-rule/update/**\",\"GET /orders\",\"POST/dept/tree\",\"PUT/user\",\"PUT /nursingLevel/update\",\"POST /visit\",\"POST/iot/RegisterDevice\",\"PUT/nursing_project/**\",\"GET/trade-common-feign/query-refund\",\"PUT/nursingTask/updateTime\",\"PUT/device-data/update/**\",\"PUT/roomTypes/**\",\"GET/contract/**\",\"DELETE/nursing/plan/**\",\"PUT/post\",\"PUT/elder/cancel\",\"POST/bill\",\"GET/checkIn\",\"PATCH/post\",\"POST/nursing/plan\",\"GET /bill/**\",\"DELETE/nursing_project/**\",\"DELETE/resource/**\",\"DELETE/dept/**\",\"POST/nursing_project\",\"PUT/checkIn/cancel\",\"DELETE/room/delete/**\",\"DELETE/user/remove/**\",\"POST/resource/enable\",\"POST/nursingLevel/insert\",\"PUT/bill\",\"GET/nursingTask\",\"POST/room/add\",\"POST /orders/refund\",\"POST/floor/add\",\"PATCH/dept\",\"DELETE/bed/delete/**\",\"POST/bill/prepaidRechargeRecord\",\"POST/iot/QueryDeviceDetail\",\"PUT/bed/update\",\"POST/user/reset-passwords/**\",\"PATCH/role\",\"GET/elder\",\"PUT/role\",\"PUT/resource\",\"GET /user/current-user\",\"POST/orders/**\",\"POST/checkIn/create\",\"PUT/nursingLevel/**\",\"PUT/nursingTask/cancel\",\"GET/bill/arrears/\",\"POST/elder/create\",\"PATCH/user\",\"PUT /floor/update\",\"PUT/nursing/**\",\"PUT/reservation/**\"],\"roleLabels\":[\"admin\"],\"sex\":\"0\",\"updateBy\":1671403256519078079,\"updateTime\":\"2023-07-12T16:26:01\",\"userType\":\"0\",\"username\":\"admin@qq.com\"}");
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserThreadLocal.removeSubject();
    }
}
